<?php

	/**
	 * Realestatemanager scroller module for Realestatemanager 
	 * @version 2.1
	 * mod_realstate_manager_extL
	 * rewriting for Joomla 1.5 by Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru), 2010;
	*/
	if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );


	defined( '_JEXEC' ) or die( 'Restricted access' );
	$database = &JFactory::getDBO();

	$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path']	= JPATH_SITE;
	require_once ($mosConfig_absolute_path."/administrator/components/com_realestatemanager/admin.realestatemanager.class.others.php");
	require_once($mosConfig_absolute_path."/administrator/components/com_realestatemanager/admin.realestatemanager.class.conf.php");
	
	
	if ( !function_exists('getWhereUsergroupsString')) {
	  function getWhereUsergroupsString( $table_alias ) { 
		global $my;
		 
		if ( isset($my->id) AND $my->id != 0 ) { 
		 
		  $usergroups_sh = getGroupsByUser ($my->id,'');       
	  
		  //$usergroups_sh = '-2'.$usergroups_sh;   
		  
		} else {
		   $usergroups_sh = array ();
		}
		$usergroups_sh[] = -2; 
		  
			  
		$s = '';
		for ($i=0; $i<count($usergroups_sh); $i++) {  
		  $g = $usergroups_sh[$i];
		  $s .= " $table_alias.params LIKE '%,{$g}' or $table_alias.params = '{$g}' or $table_alias.params LIKE '{$g},%' or $table_alias.params LIKE '%,{$g},%' ";
		  if ( ($i+1)<count($usergroups_sh) )
			$s .= ' or ';
		} 
		return $s;
	  } 
	}

	$s=GetWhereUserGroupsString("c");

	if( !function_exists( 'sefreltoabs')) {
	  function sefRelToAbs( $value ) {
		//Need check!!!

		// Replace all &amp; with & as the router doesn't understand &amp;
		$url = str_replace('&amp;', '&', $value);
		if(substr(strtolower($url),0,9) != "index.php") return $url;
		$uri    = JURI::getInstance();
		$prefix = $uri->toString(array('scheme', 'host', 'port'));
		return $prefix.JRoute::_($url);
	  }
	}

	//Common parameters

	$sort_top_by    = $params->get('sort_by_top', 1);        //Get how to sort the top items
	$show_published    = $params->get('only_published', 1);    //Get if we only show published items
	$cover_height    = $params->get('cover_height', "100");     //Get Cover Height
	$count_realestate = intval($params->def('books',1)); 
	$show_ranking    = $params->def('ranking', 0 ); 
	$show_covers     = $params->def('covers', 0 );
	$show_extra        = $params->def('extras', 1 );    
	$ItemId_tmp_from_params=$params->get('ItemId');
	$moduleclass_sfx = $params->get('moduleclass_sfx', '');

	$database = &JFactory::getDBO();
	$my=&JFactory::getUser();
	$mosConfig_live_site=JURI::base();
	$acl =& JFactory::getACL();
	$document=& JFactory::getDocument();
	$mainframe = &JFactory::getApplication();


	$document->addStyleSheet( $mosConfig_live_site.'/modules/mod_realestatemanager_scroller_free/includes/scrollable-minimal.css' );
	$document->addStyleSheet( $mosConfig_live_site.'/modules/mod_realestatemanager_scroller_free/includes/scrollable-navig.css' );

	
	//Definition of Sorts
	switch($sort_top_by)
	{
		case 0:
			$sql_sort_top = "hits";
			break;
		case 1:
			$sql_sort_top = "date";
			break;
	}

	 
	//Check if only display published items
	if ($show_published==1) {
		$sql_published = "AND h.published=1";
	} else {
		$sql_published = "";
	}




	 if($count_realestate!='' && $count_realestate!=0) {
		$selectstring = "SELECT h.htitle,h.id,h.image_link,h.hits,hc.idcat, h.price 
				\nFROM #__rem_houses AS h
				\nLEFT JOIN #__rem_categories AS hc ON hc.iditem=h.id
				\nLEFT JOIN #__rem_main_categories AS c ON c.id=hc.idcat
				\nWHERE ($s) ".$sql_published." AND h.is_featured=1"." ORDER BY ".$sql_sort_top." DESC LIMIT 0, $count_realestate;";
		$database->setQuery($selectstring);
		$rows_realestate= $database->loadObjectList();
	}

    $selectstring = "SELECT id  FROM #__menu WHERE menutype like '%menu%' AND link LIKE '%option=com_realestatemanager%'";
	
    $database->setQuery($selectstring);
    $ItemId_tmp_from_db = $database->loadResult();
	
    if($ItemId_tmp_from_params!=''){
      $ItemId_tmp=$ItemId_tmp_from_params;
    }
    else{
      $ItemId_tmp=$ItemId_tmp_from_db;
    }
	
	$i = 0;
	
?>


<div class="realestatemanager_<?php if($moduleclass_sfx!='') echo $moduleclass_sfx;?>">

	<div id="slider">
	
		<?php 

			// var_dump($rows_realestate);
			
			foreach ($rows_realestate as $row) {

				// link to detail
				$link1 = JURI::base().'/index.php?option=com_realestatemanager&amp;task=view&amp;id='.$row->id.'&amp;catid='.$row->idcat.'&amp;Itemid='.$ItemId_tmp;

				// title for the property
				$title = $row->htitle;
				
				// truncate title
				if(strlen($title)>42){
					$title = substr($title, 0, 42);
				}
														   
				$mosConfig_absolute_path=JPATH_BASE;
				$mosConfig_live_site=JURI::base(true);
				$imageURL = $row->image_link ;
				
				if($imageURL!=''){
					$file_pth= pathinfo($imageURL);
					$file_type=".".$file_pth['extension'];
					if(array_key_exists  ( 'filename' , $file_pth  ) ) $file_name=$file_pth['filename'];
					else $file_name = substr($imageURL, 0,strlen($imageURL)-strlen($file_pth['extension'] ) -1 );
					
					/*$file=$mosConfig_absolute_path . '/components/com_realestatemanager/photos/'. $file_name ."_gallery". $file_type;
					if(!file_exists($file)){
					  $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name."_mini".$file_type;
					}
					else $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name."_gallery".$file_type;*/
					
					$file=$mosConfig_absolute_path . '/components/com_realestatemanager/photos/'. $file_name . $file_type;
					if(!file_exists($file)){
					  $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name . $file_type;
					}
					else $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name . $file_type;
					
					$imageURL=$file;
				}
				else {
					$imageURL = "./components/com_realestatemanager/images/no-img_eng.gif";
				}

				
				if ( $show_extra == 1 ) { 
					$extras= "({$row->hits})"; 
				}

				else {
					$extras="";
				}
				
				$price = '';
				
				
				if( $row->price!='' && $realestatemanager_configuration['price']['show'] == '1'){
					$price = number_format($row->price, 0, ",", ".");
				}
				
		?>
				
				<img src="<?php echo $imageURL; ?>" alt="" title="<strong><?php echo $title; ?></strong><span>Preço: <?php echo $price; ?> € &nbsp; &nbsp; &nbsp; <a href='<?php echo sefRelToAbs($link1); ?>'>Ver mais</a></span>">
							
				
				<?php 
			} 
			?>

	</div>
	
	<input type="hidden" name="redirect" id="redirect" value="<?php $redirect_link = ''; echo $redirect_link; ?>" />

</div>

<script type="text/javascript">

	$(document).ready(function() {
		
		Cufon.now();
		
		$('#slider').nivoSlider({
			effect:'sliceUpDown', //Specify sets like: 'fold,fade,sliceDown, sliceDownLeft, sliceUp, sliceUpLeft, sliceUpDown, sliceUpDownLeft'
			slices:17,
			animSpeed:500,
			pauseTime:5000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:false, //Next & Prev
			directionNavHide:false, //Only show on hover
			controlNav:true, //1,2,3...
			controlNavThumbs:false, //Use thumbnails for Control Nav
			controlNavThumbsFromRel:false, //Use image rel for thumbs
			controlNavThumbsSearch: '.jpg', //Replace this with...
			controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
			keyboardNav:true, //Use left & right arrows
			pauseOnHover:true, //Stop animation while hovering
			manualAdvance:false, //Force manual transitions
			captionOpacity:1, //Universal caption opacity
			//beforeChange: function(){$('.nivo-caption').animate({bottom:'-110'},400,'easeInBack')}, NOTE: easing is not working!!!
			//afterChange: function(){Cufon.refresh();$('.nivo-caption').animate({bottom:'-20'},400,'easeOutBack')}, NOTE: easing is not working!!!
			slideshowEnd: function(){} //Triggers after all slides have been shown
		});
		
		Cufon.refresh();
		
	});
	
</script>